package com.ketai.dao.impl;

import com.ketai.dao.BaseDao;
import com.ketai.dao.CheckNumberDao;
import com.ketai.vo.CountVo;

import javax.naming.NamingException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:胡远博
 * @Description:
 * @Date:Create In 2022-08-11 2:12
 **/
public class CheckNumberDaoImpl extends BaseDao implements CheckNumberDao {
    @Override
    public int selectUserCount() throws SQLException, NamingException {
        String sql = "select count(*) total from c_user";
        ResultSet rs = query(sql, null);
        int i = 0;
        while (rs.next()) {
            i = rs.getInt("total");
        }
        return i;
    }

    @Override
    public int selectOrganCount() throws SQLException, NamingException {
        String sql = "select count(*) total from organizationbase";
        ResultSet rs = query(sql, null);
        int i = 0;
        while (rs.next()) {
            i = rs.getInt("total");
        }
        return i;
    }

    @Override
    public int selectActivityCount() throws SQLException, NamingException {
        String sql = "select count(*) total from c_activity";
        ResultSet rs = query(sql, null);
        int i = 0;
        while (rs.next()) {
            i = rs.getInt("total");
        }
        return i;
    }

    @Override
    public List selectSubtotal() throws SQLException, NamingException {
        String sql = "SELECT a.name, COUNT(b.tid) AS count FROM activity_type a LEFT JOIN c_activity b ON a.id = b.tid GROUP BY a.id";
        ResultSet rs = query(sql, null);
        List list = new ArrayList();
        while (rs.next()) {
            CountVo countVo = new CountVo();
            countVo.setValue(rs.getInt("count"));
            countVo.setName(rs.getString("name"));
            list.add(countVo);
        }
        return list;
    }

    @Override
    public int getCountByTpe(String sql) throws SQLException, NamingException {
        ResultSet query = query(sql, null);
        return query.next() ? query.getInt("count") : 0;
    }
}
